<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/5/2
 * Time: 下午9:53
 */

namespace Home\Model;


use Think\Model;

class AdvertiseModel extends Model
{

    /**
     * 根据唯一标识获取广告数据
     */
    public function getAdvertiseListByOnlySign($onlySign)
    {
        $map = [
            'only_sign' => $onlySign,
            'status' => 1,
        ];
        $position = D('AdvertisePosition')->where($map)->find();

        $data = [];

        if (empty($position)) {
            return [];
        }
        switch ($position['type']) {
            case '1':
                $where = [
                    'position_id' => $position['id'],
                    'status' => 1
                ];
                $data = D('AdvertiseData')->where($where)->order('sort ASC, id DESC')->limit(1)->select();
                break;

            case '2':
                $where = [
                    'position_id' => $position['id'],
                    'status' => 1
                ];
                $data = D('AdvertiseData')->where($where)->order('sort ASC, id DESC')->limit(1)->select();
                foreach ($data as $k=>&$v) {
                    $image = json_decode($v['content'], true);
                    $v['image_url'] = $image ? $image['image_url'] : '';
                }
                break;

            case '3':
                $where = [
                    'position_id' => $position['id'],
                    'status' => 1
                ];
                $data = D('AdvertiseData')->where($where)->order('sort ASC, id DESC')->select();
                foreach ($data as $k=>&$v) {
                    $image = json_decode($v['content'], true);
                    $v['image_url'] = $image ? $image['image_url'] : '';
                }
                break;
        }

        $position['data'] = $data;

        return $position;
    }

}